package com.cloudfast.platform.region.service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudfast.common.page.Page;
import com.cloudfast.platform.region.dao.RegionCityDao;
import com.cloudfast.platform.region.entity.RegionCity;

/**
 * 指正region service
 *
 * @author liuyw
 */
@Service
public class RegionService {
    @Autowired
    private RegionCityDao regionDao;

    public RegionCity findOne(Long id) {
        return regionDao.findOne(id);
    }

    /**
     * 分页
     *
     * @param page
     * @param entity
     * @return
     */
    public Page<RegionCity> findPage(Page<RegionCity> page, RegionCity entity) {
        StringBuilder hql = new StringBuilder(" from RegionCity ");
        return this.regionDao.findPage(page, hql.toString());
    }

    /**
     * 下级
     *
     * @param parentCode
     * @return
     */
    public List<RegionCity> selectTree(String parentCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parentCode", parentCode);
        StringBuilder hql = new StringBuilder("from RegionCity where 1=1 and parentCode=:parentCode");

        return this.regionDao.findList(hql.toString(), params);
    }

    public RegionCity getRegionCity(String region) {
        return regionDao.getRegionCity(region);
    }

    /**
     * 根据车牌号查询车牌所属地
     *
     * @param carNo
     * @return
     */
    public RegionCity findCarSourceAddr(String carNo) {
        return regionDao.findRegionCityByCarNoSign(carNo.substring(0, 2));
    }

    /**
     * 根据车牌号查询车牌所属地
     *
     * @param regionName
     * @param regionType
     * @return
     */
    public RegionCity findRegionCityByRegionName(String regionName, String regionType) {
        return regionDao.findRegionCityByRegionName(regionName, regionType);
    }

    /**
     * 查询用户下的所属区县;地市 * @param regionCode * @param regionType
     *
     * @return
     */
    public List<RegionCity> findUserHasRegionCity(String regionCode, String regionType) {
        StringBuilder hql = new StringBuilder("from RegionCity where 1=1 ");
        hql.append(" and gradeCode like :gradeCode");
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("gradeCode", "%" + regionCode + "%");
        hql.append(" and regionType=:regionType");
        RegionCity regionCity = this.getRegionCity(regionCode);
        if (StringUtils.isEmpty(regionType)) {
            if (regionCity.getRegionType().equals("C") || regionCity.getRegionType().equals("D")) {
                regionType = "D";
            } else {
                regionType = "C";
            }
        }
        params.put("regionType", regionType);
        return this.regionDao.findList(hql.toString(), params);
    }

    public List<RegionCity> findRegionCityList() {
        StringBuilder hql = new StringBuilder("from RegionCity where 1=1 ");

        return this.regionDao.findList(hql.toString());
    }

}
